﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using WpfApp4BlogReader.Models;
using WpfControlLibrary4MVVMLib;

namespace WpfApp4BlogReader.ViewModels
{
    public class MainVM : ViewModelBase<IEnumerable<Blog>>
    {
        ICommand _undoCommand, _redoCommand;

        public MainVM(IEnumerable<Blog> blogs)
        {
            Model = new ObservableCollection<Blog>(blogs);
            UndoManager = new UndoManager();
        }

        public UndoManager UndoManager { get; private set; }

        public ICommand UndoCommand
        {
            get
            {
                return _undoCommand
                    ?? (_undoCommand = new RelayCommand(
                        () => UndoManager.Undo(), () => UndoManager.CanUndo));
            }
        }

        public ICommand RedoCommand
        {
            get
            {
                return _redoCommand
                    ?? (_redoCommand = new RelayCommand(
                        () => UndoManager.Redo(), () => UndoManager.CanRedo));
            }
        }

        public IEnumerable<BlogVM> Blogs
        {
            get
            {
                return Model.Select(blog => new BlogVM(blog, this));
                //return Model.Select(blog => new BlogVM { Model = blog });
            }
        }

        BlogVM _selectedBlog;
        public BlogVM SelectedBlog
        {
            get { return _selectedBlog; }
            set
            {
                if (SetProperty(ref _selectedBlog, value, () => SelectedBlog))
                    OnPropertyChanged("IsSelectedBlog");
            }
        }

        public Visibility IsSelectedBlog
        {
            get
            {
                return SelectedBlog != null
                    ? Visibility.Visible
                    : Visibility.Collapsed;
            }
        }

    }
}
